package com.definesys.smecrailtransit.adapter.LineStationAdapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutMonthFaultItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutMonthFormOmitItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutPopNullDataBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.MonthForm.bean.FaultStationBean;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.dialog.SiteReaErrorDialog;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by cjy on 2018/3/9.
 */

public class LineStationFaultAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int NORMAL = 1;
    private static int DOT = -1;
    private static int NULL = 0;

    private ArrayList<FaultStationBean> list;
    private DisplayMetrics dm;
    private int maxNumber1;
    private int maxNumber2;
    protected Bus mRxbus;
    private String type;
    private Context mContext;
    private SiteDetailsPresenter siteDetailsPresenter;
    private SiteReaErrorDialog siteReaErrorDialog;
    public boolean showAnimation = true;

    private HashMap<String,Integer> hashMap=new HashMap<>();

    public LineStationFaultAdapter(DisplayMetrics dm, Bus mRxbus, String type, Context context){
            list=new ArrayList<>();
            this.mContext=context;
            this.dm = dm;
            this.mRxbus = mRxbus;
            this.type = type;

            if(type !=null && type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                siteDetailsPresenter=new SiteDetailsPresenter(mContext);
            }
    }

    public void setList(ArrayList<FaultStationBean> list) {
        if(list != null && list.size() != 0){
            this.list = list;
            this.maxNumber1 = this.list.get(0).getFtValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber1 < this.list.get(i).getFtValue() && this.list.get(i).getSeq() != -1){
                    maxNumber1 = this.list.get(i).getFtValue();
                }
            }

            this.maxNumber2 = this.list.get(0).getNotselfValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber2 < this.list.get(i).getNotselfValue() && this.list.get(i).getSeq() != -1){
                    maxNumber2 = this.list.get(i).getNotselfValue();
                }
            }
        }else{
            this.list = new ArrayList<>();
        }

        if(maxNumber1 > 20){
            maxNumber1 = 20;
        }

        if(maxNumber2 > 20){
            maxNumber2 = 20;
        }
        notifyDataSetChanged();
    }

    public ArrayList<FaultStationBean> getList(){
        return this.list;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NULL) {
            LayoutPopNullDataBinding layoutPopNullDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_pop_null_data, parent, false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder = new
                    SmecBaseRecyclerViewHolder(layoutPopNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutPopNullDataBinding);
            return smecBaseRecyclerViewHolder;
        } else if (viewType == NORMAL) {

                LayoutMonthFaultItemBinding layoutMonthFaultItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                        , R.layout.layout_month_fault_item, parent, false);
                SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder = new
                        SmecBaseRecyclerViewHolder(layoutMonthFaultItemBinding.getRoot());
                smecBaseRecyclerViewHolder.setViewType(viewType);
                smecBaseRecyclerViewHolder.setBinding(layoutMonthFaultItemBinding);
                return smecBaseRecyclerViewHolder;

        } else {
            LayoutMonthFormOmitItemBinding layoutMonthFormItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_month_form_omit_item, parent, false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder = new
                    SmecBaseRecyclerViewHolder(layoutMonthFormItemBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutMonthFormItemBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

@Override
public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
    holder.setIsRecyclable(false);
    if(holder.getViewType() == NORMAL){

        LayoutMonthFaultItemBinding viewDataBinding=(LayoutMonthFaultItemBinding)holder.getBinding();
        viewDataBinding.site.setText(list.get(position).getName());


        viewDataBinding.number1.setText(String.valueOf(list.get(position).getFtValue())+"次");
        viewDataBinding.number2.setText(String.valueOf(list.get(position).getNotselfValue())+"次");

        viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(LineStationFaultAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                    HashMap<String,Object> map = new HashMap<>();
                    map.put("ftValue",list.get(position).getFtValue());
                    map.put("notValue",list.get(position).getNotselfValue());
                    map.put("lineId",list.get(position).getLineId());
                    map.put("stationId",list.get(position).getStationId());
                    map.put("stationName",list.get(position).getName());
                    mRxbus.post(MonthFormFragment.GET_DETAIL, map);
                }
            }
        });

        int width1 = 0 ;
        int width2 = 0 ;

        try {
            if( this.list.get(position).getFtValue() < this.maxNumber1){
                width1 = this.dm.widthPixels*2/5*this.list.get(position).getFtValue()/this.maxNumber1;
            }else{
                width1 = this.dm.widthPixels*2/5;
            }

            if( this.list.get(position).getNotselfValue() < this.maxNumber2){
                width2 = this.dm.widthPixels*2/5*this.list.get(position).getNotselfValue()/this.maxNumber1;
            }else{
                width2 = this.dm.widthPixels*2/5;
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        if(maxNumber1 != 0){
            if(this.showAnimation){
                Animation(viewDataBinding.pillar1,width1);
            }else{
                viewDataBinding.pillar1.getLayoutParams().width = width1;
            }
        }else{
            viewDataBinding.pillar1.getLayoutParams().width = 0;
        }

        if(maxNumber2 != 0){
            if(this.showAnimation){
                Animation(viewDataBinding.pillar2,width2);
            }else{
                viewDataBinding.pillar2.getLayoutParams().width = width2;
            }
        }else{
            viewDataBinding.pillar2.getLayoutParams().width = 0;
        }

        viewDataBinding.invalidateAll();
    }
}


    @Override
    public int getItemCount() {
        if(list.size() == 0 ){
            return 1;
        }else {
            return this.list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
            if(list.size() == 0){
                return NULL;
            }else {
                if(this.list.get(position).getSeq() != -1){
                return NORMAL;
            }else {
                return DOT;
            }
        }
    }

    private void showDialog(int id,String name){
        hashMap.put("stationId",id);
        siteDetailsPresenter.getRealTimeStatus(hashMap);
        if(siteReaErrorDialog == null){
            siteReaErrorDialog=new SiteReaErrorDialog(mContext,R.style.project_back,false,siteDetailsPresenter);
        }
        siteReaErrorDialog.setStationName(name);
        siteReaErrorDialog.show();
    }

    private void Animation(View v,int width){
        //属性动画对象
        ValueAnimator va ;
        //显示view，高度从0变到height值
        va = ValueAnimator.ofInt(0,width);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    //获取当前的height值
                    int w =(Integer)valueAnimator.getAnimatedValue();
                    //动态更新view的高度
                    v.getLayoutParams().width = w;
                    v.requestLayout();
                    }
            });
        va.setDuration(800);
        //开始动画
        va.start();
    }
}

